<?php

/**
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 * Copyright (c) 2010 Mr. Ynk: http://www.mrynk.nl
 */

/**
 * @author Michel Ypma: michel@mrynk.nl
 */

class BuilderUtils
{
    public static function className($str)
    {
        $str = str_replace(array('-', '_'), ' ', $str);
        $str = ucwords($str);
        $str = str_replace(' ', '', $str);

        return $str;
    }

    public static function methodName($str)
    {
        $str = str_replace(array('-', '_'), ' ', $str);
        $str = ucwords($str);
        $str = str_replace(' ', '', $str);

        return $str;
    }

    public static function attributeName($str)
    {
        return $str;
    }

    public static function parameterName($str)
    {
        $str = strtolower($str);

        return $str;
    }

    public static function indent($contents)
    {
        foreach ($contents as $k => $content)
        {
            $str        = '';
            $tabs       = 0;
            $lines      = explode("\n", $content);

            foreach ($lines as $line)
            {
                $line = trim($line);

                if (preg_match('/^\}/', $line) && $tabs > 0) { $tabs --; }

                $str .= str_repeat(' ', $tabs * 4).$line."\n";

                if (preg_match('/\{$/', $line)) { $tabs ++; }
            }

            $contents[$k] = $str;
        }

        return $contents;
    }

    public static function writeFile($target, $contents, $overwrite = true)
    {
        $path = '';

        $dirs = explode('/', $target);

        $c = count($dirs) - 1;

        for ($i = 0; $i < $c; $i ++)
        {
            $path .= $dirs[$i].'/';

            if (!is_dir($path))
            {
                mkdir($path);
            }
        }
	$path = $path.preg_replace('/[0-9]$/', '', trim( end($dirs) ) );
        if ($overwrite === true || !file_exists($path))
        {
			MytoCLI::output( 'overwrite: '.$overwrite.'>> '.$path );
            $handle = fopen($path, 'w');
            fwrite($handle, $contents);
            fclose($handle);
        }
    }
}
?>